package com.example.aslongastoday.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.aslongastoday.R;
import com.example.aslongastoday.databinding.ViewBacklogGroupBinding;
import com.example.aslongastoday.db.dao.BackLogGroupDao;
import com.example.aslongastoday.domain.BackLogGroup;
import com.example.aslongastoday.ui.backlog.EditBacklogFragment;

import java.util.List;


public class ViewBackLogGroupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<BackLogGroup> backLogGroupList;
    private final Context context;
    private final BackLogGroupDao dao;
    private final Fragment fragment; //这里的主fragment是主待办模块的那个fragment
    private EditBacklogFragment editBacklogFragment;

    private PopupMenu popupMenu;//菜单视图

    public ViewBackLogGroupAdapter(List<BackLogGroup> items, Context context, Fragment fragment) {
        backLogGroupList = items;
        this.context = context;
        dao = BackLogGroupDao.getInstance(context);
        this.fragment = fragment;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new ViewHolder(ViewBacklogGroupBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder1, int position) {
        //        holder.mItem = mValues.get(position);
        ViewHolder holder = (ViewHolder) holder1;

        holder.groupName.setText(backLogGroupList.get(position).getGroupName());
        holder.backlogItemList.setLayoutManager(new LinearLayoutManager(context));
        holder.backlogItemList.setAdapter(new ViewBackLogAdapter(backLogGroupList.get(position).getBackLogList()
                , context, backLogGroupList.get(position).getId(),fragment));
//        holder.mContext.setText(mValues.get(position).getContent());
//        holder.mDDL.setText(mValues.get(position).getDDL());

        // 通过视图绑定的位置来获取当前的待办组对象
        BackLogGroup backLogGroup = backLogGroupList.get(position);
        // 获取了当前的待办组的id
        Integer backLogGroupId = backLogGroup.getId();
        // 获取了当前的待办组的name
        String backLogGroupName = backLogGroup.getGroupName();

        /*为待办组两个按钮添加监听*/
        holder.iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editBacklogFragment = new EditBacklogFragment();
//                Toast.makeText(context, ""+backLogGroupId, Toast.LENGTH_SHORT).show();
                //添加新的待办项
                Bundle bundle = new Bundle();
                String type = "add_backlog";
                bundle.putString("type", type);
                bundle.putInt("id", backLogGroupId);
                editBacklogFragment.setArguments(bundle);
                FragmentManager fragmentManager = fragment.getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.container, editBacklogFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });

        int i = position;

        // 待办组的功能按键监听
        holder.iv_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupMenu = new PopupMenu(v.getContext(), holder.iv_list); //创建PopupMenu对象
                popupMenu.inflate(R.menu.backlog_group_menu); //加载菜单资源文件
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                    private FragmentTransaction fragmentTransaction;
                    private FragmentManager fragmentManager;
                    private String type;
                    private Bundle bundle;

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            //编辑待办组名称
                            case R.id.it_editBacklogGroup:
                                editBacklogFragment = new EditBacklogFragment();
                                //数据打包
                                bundle = new Bundle();
                                type = "edit_backlog_group";
                                bundle.putString("type", type);
                                bundle.putInt("id", backLogGroupId);
                                bundle.putString("name",backLogGroupName);
                                editBacklogFragment.setArguments(bundle);
                                //fragment跳转
                                fragmentManager = fragment.getActivity().getSupportFragmentManager();
                                fragmentTransaction = fragmentManager.beginTransaction();
                                fragmentTransaction.add(R.id.container, editBacklogFragment);
                                fragmentTransaction.addToBackStack(null);
                                fragmentTransaction.commit();
                                break;
                            case R.id.it_deleteBacklogGroup:
                                //TODO 删除待办组
                                dao.delete(backLogGroupList.get(i).getId());
                                backLogGroupList.remove(i);
                                notifyDataSetChanged();
                                Toast.makeText(context, "删除待办组", Toast.LENGTH_SHORT).show();
                                break;
                            default:
                                break;
                        }
                        return true;
                    }
                });
                popupMenu.show(); //显示菜单
            }
        });
    }


    @Override
    public int getItemCount() {
        return backLogGroupList==null?0:backLogGroupList.size();
    }

    public List<BackLogGroup> getBackLogGroupList() {
        return backLogGroupList;
    }

    public void setBackLogGroupList(List<BackLogGroup> backLogGroupList) {
        this.backLogGroupList = backLogGroupList;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView groupName;
        //        public final TextView mContext;
//        public final TextView mDDL;
//        public BackLog mItem;
        public final RecyclerView backlogItemList;
        /*待办组的两个图标按钮*/
        public final ImageView iv_add;
        public final ImageView iv_list;


        public ViewHolder(ViewBacklogGroupBinding binding) {
            super(binding.getRoot());
            groupName = binding.backlogGroupName;
            backlogItemList = binding.backlogItemListRv;
//            mContext = binding.content;
//            mDDL = binding.ddl;
            /*绑定待办组的两个图标按钮*/
            iv_add = binding.ivAdd;
            iv_list = binding.ivList;
        }

        @Override
        public String toString() {
            return "";
        }
    }

}